
import java.util.Iterator;
import java.util.NoSuchElementException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * @author WORKSPOT2
 */
public class RandomizedQueue<Item> implements Iterable<Item> {

    private Item[] storeArray;
    private int size;

    public RandomizedQueue() // construct an empty randomized queue
    {
        storeArray = (Item[]) new Object[10];
        size = 0;
    }

    public boolean isEmpty() // is the queue empty?
    {
        return storeArray.length == 0;
    }

    public int size() // return the number of items on the queue
    {
        return size;
    }

    public void enqueue(Item item)  // add the item
    {
        if (item == null)
            throw new NullPointerException();
        if (size == storeArray.length) {
            resize(size * 2);
        }
        storeArray[size++] = item;
    }

    public Item dequeue() // delete and return a random item
    {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        int index;
        Item toReturn;
        do {
            index = StdRandom.uniform(0, size);
            toReturn = storeArray[index];
        } while (toReturn == null);
        storeArray[index] = null;
        size--;
        if (size > 0 && size == storeArray.length / 4) {
            resize((size / 2) + 1);
        }
        return toReturn;
    }

    private void resize(int newSize) {
        Item[] newArray = (Item[]) new Object[newSize];
        int l = 0;
        for (int i = 0; i < storeArray.length; i++) {
            if (storeArray[i] != null) {
                newArray[l++] = storeArray[i];
            }
        }
        storeArray = newArray;
    }

    public Item sample() // return (but do not delete) a random item
    {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        int index;
        Item toReturn = null;
        do {
            index = StdRandom.uniform(0, size);
            toReturn = storeArray[index];
        } while (toReturn == null);
        return toReturn;
    }

    public Iterator<Item> iterator() // return an independent iterator over items in random order
    {
        return new RandomIterator();
    }

    private class RandomIterator implements Iterator<Item> {

        private int[] existIndex;
        private int counter = 0;

        private RandomIterator() {
            existIndex = new int[size];
            fillExitsIndexes();
            StdRandom.shuffle(existIndex);
        }

        private void fillExitsIndexes() {
            int k = 0;
            for (int i = 0; i < storeArray.length; i++) {
                if (storeArray[i] != null) {
                    existIndex[k++] = i;
                }
            }
        }

        @Override
        public boolean hasNext() {
            return counter < existIndex.length;
        }

        @Override
        public Item next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            Item toReturn = storeArray[existIndex[counter]];
            counter++;
            return toReturn;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    public static void main(String[] args) {  // unit testing
        RandomizedQueue<String> queue = new RandomizedQueue<String>();
        for (int i = 0; i < 10; i++) {
            queue.enqueue("Item " + i);
        }
        String d1 = queue.dequeue();
        System.out.println("D1: " + d1);
        String d2 = queue.dequeue();
        System.out.println("D2: " + d2);
        Iterator<String> iter = queue.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }
}
